<template>
  <div class="l-rblock l-form-viewer" style="padding: 8px">
    <el-form
      :model="formData"
      :rules="rules"
      size="mini"
      labelPosition="right"
      labelWidth="80px"
      ref="form"
    >
      <div class="l-auto-window only-tabs">
        <el-tabs v-model="activeName" type="card">
          <el-tab-pane
            class="l-tabs-container"
            :label="$t('主表信息')"
            name="tab0"
          >
            <el-row :gutter="0">
              <div class="l-rblock">
                <el-col
                  v-if="lr_formLookAuth('case_erp_deviceinfo_f_Type')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('设备类型')"
                    prop="case_erp_deviceinfo_f_Type"
                  >
                    <l-select
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_deviceinfo_f_Type',
                          false || isReadOnly
                        )
                      "
                      :options="lr_dataItem['DeviceType']"
                      labelKey="f_ItemName"
                      valueKey="f_ItemValue"
                      v-model="formData.case_erp_deviceinfo_f_Type"
                    />
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_deviceinfo_f_Number')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('设备编号')"
                    prop="case_erp_deviceinfo_f_Number"
                  >
                    <el-input
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_deviceinfo_f_Number',
                          isReadOnly
                        )
                      "
                      v-model="formData.case_erp_deviceinfo_f_Number"
                      :placeholder="$t('please enter')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_deviceinfo_f_Name')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('设备名称')"
                    prop="case_erp_deviceinfo_f_Name"
                  >
                    <el-input
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_deviceinfo_f_Name',
                          isReadOnly
                        )
                      "
                      v-model="formData.case_erp_deviceinfo_f_Name"
                      :placeholder="$t('please enter')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_deviceinfo_f_Model')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('规格型号')"
                    prop="case_erp_deviceinfo_f_Model"
                  >
                    <el-input
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_deviceinfo_f_Model',
                          isReadOnly
                        )
                      "
                      v-model="formData.case_erp_deviceinfo_f_Model"
                      :placeholder="$t('please enter')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_deviceinfo_f_Supplier')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('供应商')"
                    prop="case_erp_deviceinfo_f_Supplier"
                  >
                    <l-layer-select
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_deviceinfo_f_Supplier',
                          false || isReadOnly
                        )
                      "
                      :isPage="true"
                      :height="504"
                      :width="960"
                      :columns="[
                        {
                          label: '供应商编码',
                          prop: 'f_number',
                          valueKey: '',
                          minWidth: 120,
                          align: 'left',
                          hidden: false,
                          isNotAutoWrap: true,
                        },
                        {
                          label: '供应商名称',
                          prop: 'f_name',
                          valueKey: '',
                          minWidth: 150,
                          align: 'left',
                          hidden: false,
                          isNotAutoWrap: true,
                        },
                        {
                          label: '负责人',
                          prop: 'f_person',
                          valueKey: '',
                          minWidth: 100,
                          align: 'left',
                          hidden: false,
                        },
                        {
                          label: '手机号',
                          prop: 'f_phone',
                          valueKey: '',
                          minWidth: 100,
                          align: 'left',
                          hidden: false,
                        },
                        {
                          label: '经营范围',
                          prop: 'f_scope',
                          valueKey: '',
                          minWidth: 100,
                          align: 'left',
                          hidden: false,
                        },
                      ]"
                      :options="case_erp_deviceinfo_f_SupplierOptions"
                      v-model="formData.case_erp_deviceinfo_f_Supplier"
                    />
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_deviceinfo_f_BuyDate')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('购买日期')"
                    prop="case_erp_deviceinfo_f_BuyDate"
                  >
                    <l-date
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_deviceinfo_f_BuyDate',
                          isReadOnly
                        )
                      "
                      :readonly="false"
                      format="yyyy-MM-dd"
                      dateType="date"
                      :clearable="true"
                      v-model="formData.case_erp_deviceinfo_f_BuyDate"
                      :placeholder="$t('please select')"
                    />
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_deviceinfo_f_MaintainDate')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('维保日期')"
                    prop="case_erp_deviceinfo_f_MaintainDate"
                  >
                    <l-date
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_deviceinfo_f_MaintainDate',
                          isReadOnly
                        )
                      "
                      :readonly="false"
                      format="yyyy-MM-dd"
                      dateType="date"
                      :clearable="true"
                      v-model="formData.case_erp_deviceinfo_f_MaintainDate"
                      :placeholder="$t('please select')"
                    />
                  </el-form-item>
                </el-col>
                <el-col
                  v-if="lr_formLookAuth('case_erp_deviceinfo_f_ScrapDate')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('报废日期')"
                    prop="case_erp_deviceinfo_f_ScrapDate"
                  >
                    <l-date
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_deviceinfo_f_ScrapDate',
                          isReadOnly
                        )
                      "
                      :readonly="false"
                      format="yyyy-MM-dd"
                      dateType="date"
                      :clearable="true"
                      v-model="formData.case_erp_deviceinfo_f_ScrapDate"
                      :placeholder="$t('please select')"
                    />
                  </el-form-item>
                </el-col>

                <el-col
                  v-if="lr_formLookAuth('case_erp_deviceinfo_f_Principal')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('负责人')"
                    prop="case_erp_deviceinfo_f_Principal"
                  >
                    <l-user-select
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_productlog_f_Principal',
                          isReadOnly
                        )
                      "
                      v-model="formData.case_erp_deviceinfo_f_Principal"
                    />
                  </el-form-item>
                </el-col>

                <el-col
                  v-if="lr_formLookAuth('case_erp_deviceinfo_f_Address')"
                  :span="24"
                >
                  <el-form-item
                    :label="$t('设备位置')"
                    prop="case_erp_deviceinfo_f_Address"
                  >
                    <el-input
                      :disabled="
                        !lr_formEditAuth(
                          'case_erp_deviceinfo_f_Address',
                          isReadOnly
                        )
                      "
                      v-model="formData.case_erp_deviceinfo_f_Address"
                      :placeholder="$t('please enter')"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
              </div>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-form>
  </div>
</template>
<script>
const api = window.$api.erpcase.deviceinfo;

export default {
  props: {
    isReadOnly: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      moduleCode: "ERP_DeviceInfo",
      activeName: "tab0",
      formData: {
        case_erp_deviceinfo_f_Id: "", // 唯一标识(GUID)
        case_erp_deviceinfo_f_Type: "", // 设备类型
        case_erp_deviceinfo_f_Number: "", // 设备编号
        case_erp_deviceinfo_f_Name: "", // 设备名称
        case_erp_deviceinfo_f_Model: "", // 规格型号
        case_erp_deviceinfo_f_Supplier: "", // 供应商
        case_erp_deviceinfo_f_BuyDate: "", // 购买日期
        case_erp_deviceinfo_f_MaintainDate: "", // 维保日期
        case_erp_deviceinfo_f_ScrapDate: "", // 报废日期
        case_erp_deviceinfo_f_Principal: "", // 负责人
        case_erp_deviceinfo_f_Address: "", // 设备位置
      },
      rules: {
        case_erp_deviceinfo_f_Type: [
          { required: true, message: "please select设备类型" },
        ],
        case_erp_deviceinfo_f_Number: [
          { required: true, message: "please enter设备编号" },
          {
            validator: this.lr_existDbFiled,
            fieldName: "f_Number",
            keyValue: () => {
              return this.formData.case_erp_deviceinfo_f_Id;
            },
            tableName: "case_erp_deviceinfo",
            keyName: "f_Id",
            trigger: "blur",
          },
        ],
        case_erp_deviceinfo_f_Name: [
          { required: true, message: "please enter设备名称" },
          {
            validator: this.lr_existDbFiled,
            fieldName: "f_Name",
            keyValue: () => {
              return this.formData.case_erp_deviceinfo_f_Id;
            },
            tableName: "case_erp_deviceinfo",
            keyName: "f_Id",
            trigger: "blur",
          },
        ],
        case_erp_deviceinfo_f_Model: [
          { required: true, message: "please enter规格型号" },
        ],
        case_erp_deviceinfo_f_MaintainDate: [
          { required: true, message: "please select维保日期" },
        ],
        case_erp_deviceinfo_f_ScrapDate: [
          { required: true, message: "please select报废日期" },
        ],
        case_erp_deviceinfo_f_Principal: [
          { required: true, message: "please select负责人" },
        ],
      },
    };
  },

  mounted() {
    this.init();
  },

  computed: {
    case_erp_deviceinfo_f_SupplierOptions() {
      const data = this.lr_DataSource("ERP_Suppliers");
      return this.lr_DataSourceOptions(data, "f_id", "f_name");
    },
    dataItem_filedsJson() {
      return { f_ItemId: this.classifyId };
    },
  },
  methods: {
    init() {
      this.systemDATAItem();
    },

    // 数据字典
    systemDATAItem() {
      this.lr_loadDataItem("DeviceType"); // 设备类型
    },

    // Reset表单
    resetForm() {
      this.formData.case_erp_deviceinfo_f_Id = "";
      this.$formClear(this.$refs.form);
      this.activeName = "tab0";
    },
    // 校验表单
    async validateForm() {
      if (!(await this.$formValidateWraper(this.$refs.form))) {
        return false;
      }

      return true;
    },
    // 设置表单数据
    setForm(data) {
      this.$setFormData(this.formData, data, "case_erp_deviceinfo");
    },
    // 获取表单数据
    getForm() {
      return this.$getFormData(this.formData, "case_erp_deviceinfo");
    },

    async loadFormData({ keyValue /*,params,node*/ }) {
      //  keyValue 流程中相当于流程processId,params 传递参数,node 流程节点
      const data = await this.$awaitWraper(api.get(keyValue));
      if (data) {
        this.setForm(data);
        return true;
      } else {
        return false;
      }
    },

    async saveForm({ keyValue, isEdit /*,code,node*/ }) {
      // isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点
      const postData = this.getForm();
      if (isEdit) {
        // Edit
        return await this.$awaitWraper(api.update(keyValue, postData));
      } else {
        // Add
        postData.f_Id = keyValue;
        return await this.$awaitWraper(api.add(postData));
      }
    },
  },
};
</script>